package com.atguigu.ssyx.product.controller;


import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.product.Category;
import com.atguigu.ssyx.product.service.CategoryService;
import com.atguigu.ssyx.vo.product.CategoryQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-12-17
 */
@RestController
@Api(tags = "商品分类列表")
@CrossOrigin
@RequestMapping("/admin/product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    //    商品分类列表
    @ApiOperation(value = "获取商品分类分页列表")
    @GetMapping("{page}/{limit}")
    public Result getPageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "categoryQueryVo", value = "查询对象", required = false)
            CategoryQueryVo categoryQueryVo) {
        Page<Category> categoryPage = new Page<>(page, limit);
        IPage<Category> categoryIPage = categoryService.getPageList(categoryPage, categoryQueryVo);
        return Result.ok(categoryIPage);
    }

    @ApiOperation("通过ID查询获取商品分类信息")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable Long id) {
        Category category = categoryService.getById(id);
        if (category != null) {
            return Result.ok(category);
        }
        return Result.fail("数据返回有误");
    }

    @ApiOperation("保存商品分类信息")
    @PostMapping("/save")
    public Result saveData(@RequestBody Category category) {
        if (categoryService.save(category)) {
            Result.ok("数据添加成功");
        }
        return Result.fail("数据添加失败");
    }

    @ApiOperation("根据ID更新商品分类信息")
    @PutMapping("/update")
    public Result updateById(@RequestBody Category category) {
        if (categoryService.updateById(category)) {
            return Result.ok("更新数据成功");
        }
        return Result.fail("更新数据失败");
    }

    @ApiOperation("根据ID删除商品分类信息")
    @DeleteMapping("/remove/{id}")
    public Result removeById(@PathVariable Long id) {
        if (categoryService.removeById(id)) {
            return Result.ok("删除数据成功");
        }
        return Result.fail("删除数据失败");
    }

    @ApiOperation("删除批量商品分类信息")
    @DeleteMapping("/remove/")
    public Result removeRows(@RequestBody List<Long> ids) {
        if (categoryService.removeByIds(ids)) {
            return Result.ok("删除数据成功");
        }
        return Result.fail("删除数据失败");
    }

    @ApiOperation("查找所有列表数据")
    @PutMapping("/findAllList")
    public Result findAllList(@RequestBody List<Long> ids) {
        return Result.ok(categoryService.findAllList());
    }
}

